﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace DIProject.Dictionary
{
    public partial class NewAssortmentDetails : Form
    {
        Int32? _assId = null;
        Util.ActionType _actType = DIProject.Util.ActionType.NoAction;
        public NewAssortmentDetails(Util.ActionType actType, Int32? assId)
        {
            _actType = actType;
            _assId = assId;
            InitializeComponent();
        }
        Int32 or_id = Int32.MinValue;
        DateTime or_md;
        private Int32 _CreatedAssortment;

        public Int32 CreatedAssortment
        {
            get { return _CreatedAssortment; }
            set { _CreatedAssortment = value; }
        }
        private void InitComboboxes()
        {
            cbCatalog.SelectedValue = 1;
            cbAssesGroup.SelectedValue = 1;
            cbMedForm.SelectedValue = 1;
        }
        private void NewAssortmentDetails_Load(object sender, EventArgs e)
        {
            InitDDLS();
            switch (this._actType)
            {
                case DIProject.Util.ActionType.Insert:
                    InitAdd();
                    break;
                case DIProject.Util.ActionType.Update:
                    InitModofi();
                    break;
                default:
                    break;
            }
            panel1.Visible = Data.Sync.IsAdmin;
            
          //  InitComboboxes();
         //   cbCatalog.SelectedValue = 1000;
        }
        private void InitAdd()
        {
            DataSets.NewDist.AssortmentRow ar = newDist1.Assortment.NewAssortmentRow();
            ar.Id = 0;
            ar.Name = string.Empty;
            newDist1.Assortment.Rows.Add(ar);
            btnInsertUpdate.Text = "Сохранить";
            InitComboboxes();
        }
        private void InitModofi()
        {
            btnInsertUpdate.Text = "Изменить";
            try
            {
                this.assortmentTableAdapter.Fill(this.newDist1.Assortment, this._assId.Value);
                DataSets.NewDist.AssortmentRow ar = (bs.Current as DataRowView).Row as DataSets.NewDist.AssortmentRow;
                or_id = ar.Id;
                or_md = ar.MD;
                SetComboboxes(ar.AssesmentGroupId, ar.CatalogId, ar.MedFormId);
            }
            catch (System.Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.Message);
            }
        }
        void InitDDLS()
        {
            cbAssesGroup.DataSource = DIProject.Data.AssortmentGroup.Data;
            cbCatalog.DataSource = DIProject.Data.Catalog.Data;
            cbMedForm.DataSource = DIProject.Data.MedForm.Data;
        }

        private void SetComboboxes(int asg_id, int cat_id, int mform_id)
        {
            int i = 0;
            for (i = 0; i < cbAssesGroup.Items.Count; i++)
            {
                DataRowView drv = cbAssesGroup.Items[i] as DataRowView;
                if (Convert.ToInt32(drv.Row.ItemArray[0]) == asg_id)
                {  // Нашли
                    cbAssesGroup.SelectedItem = cbAssesGroup.Items[i];                    
                    break;
                }
            }
            for (i = 0; i < cbCatalog.Items.Count; i++)
            {
                DataRowView drv = cbCatalog.Items[i] as DataRowView;
                if (Convert.ToInt32(drv.Row.ItemArray[0]) == cat_id)
                {  // Нашли
                    cbCatalog.SelectedItem = cbCatalog.Items[i];
                    break;
                }
            }
            for (i = 0; i < cbMedForm.Items.Count; i++)
            {
                DataRowView drv = cbMedForm.Items[i] as DataRowView;
                if (Convert.ToInt32(drv.Row.ItemArray[0]) == mform_id)
                {  // Нашли
                    cbMedForm.SelectedItem = cbMedForm.Items[i];
                    break;
                }
            }
        }

        private void txbName_Validating(object sender, CancelEventArgs e)
        {
            if (string.IsNullOrEmpty(txbName.Text))
            {
                MessageBox.Show("Обязательно");
                txbName.Select();
                e.Cancel = true;
                return;
            }
        }

        private void btnInsertUpdate_Click(object sender, EventArgs e)
        {
            switch (this._actType)
            {
                case DIProject.Util.ActionType.Update:
                    AssMidifi();
                    break;
                case DIProject.Util.ActionType.Insert:
                    AssAdd();
                    break;
                default:
                    break;
            }
        }
        private void AssMidifi()
        {
            bs.EndEdit();
            DataSets.NewDist.AssortmentRow ar = (bs.Current as DataRowView).Row as DataSets.NewDist.AssortmentRow;
            byte? bo = null;
            if (numericBeforeOverdue.Value > 0)
                bo = Convert.ToByte(numericBeforeOverdue.Value);
            decimal? rp = null;
            if (!string.IsNullOrEmpty(txbReferentPrice.Text))
            {
                decimal d;
                if (decimal.TryParse(txbReferentPrice.Text, out d))
                {
                    rp = d;
                }
            }
            DateTime? rd = null;
            if (dtpReferentDate.Checked)
                rd = dtpReferentDate.Value;
            Data.Assortment.ModifiAssortment(ar.CatalogId, ar.Name, ar.MedFormId, ar.AssesmentGroupId,
                txbListAB.Text, txbNonPatentRus.Text, txbNonPatentLat.Text, cbxGNVLS.Checked, cbxMandatory.Checked,
                txbTemperatureStore.Text, bo, cbxNoRecipe.Checked, rp, rd, txbNote.Text, DateTime.Now, FirstForm.Login,cxbDel.Checked,
                or_id,or_md);
        }
        private void AssAdd()
        {
            bs.EndEdit();
            DataSets.NewDist.AssortmentRow ar = (bs.Current as DataRowView).Row as DataSets.NewDist.AssortmentRow;    
            byte? bo = null;
            if(numericBeforeOverdue.Value > 0)
                bo = Convert.ToByte(numericBeforeOverdue.Value);
            decimal? rp = null;
            if(!string.IsNullOrEmpty(txbReferentPrice.Text))
            {
                decimal d;
                if(decimal.TryParse(txbReferentPrice.Text,out d))
                {
                    rp = d;
                }
            }
            DateTime? rd = null;
            if(dtpReferentDate.Checked)
                rd = dtpReferentDate.Value;
            this.CreatedAssortment = Data.Assortment.AddAssortment(ar.CatalogId, ar.Name, ar.MedFormId, ar.AssesmentGroupId,
                txbListAB.Text, txbNonPatentRus.Text, txbNonPatentLat.Text, cbxGNVLS.Checked, cbxMandatory.Checked,
                txbTemperatureStore.Text, bo, cbxNoRecipe.Checked, rp, rd, txbNote.Text, DateTime.Now, FirstForm.Login);
        }

        private void txbReferentPrice_Validating(object sender, CancelEventArgs e)
        {
            string str = txbReferentPrice.Text;
            if (string.IsNullOrEmpty(str))
                return;
            decimal d;
            if (!decimal.TryParse(str, out d))
            {
                e.Cancel = true;
                MessageBox.Show("введите действительное число");
                txbReferentPrice.Select();
                return;
            }
        }
    }
}